package my.graduation.config;

import my.graduation.interceptor.JwtAdminInterceptor;
import my.graduation.interceptor.JwtDoctorInterceptor;
import my.graduation.interceptor.JwtPatientInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: RavenLyn
 * @Date: 2024-11-27
 */
@Configuration
public class WebConfig implements WebMvcConfigurer{
    /**
     * 跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080","http://localhost:9090")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Autowired
    private JwtAdminInterceptor jwtAdminInterceptor;
    @Autowired
    private JwtDoctorInterceptor jwtDoctorInterceptor;
    @Autowired
    private JwtPatientInterceptor jwtPatientInterceptor;

    /**
     * 注册自定义拦截器
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(jwtAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/alipay/**");

        registry.addInterceptor(jwtDoctorInterceptor)
                .addPathPatterns("/doctor/**")
                .excludePathPatterns("/doctor/login")
                .excludePathPatterns("/alipay/**");

        registry.addInterceptor(jwtPatientInterceptor)
                .addPathPatterns("/patient/**")
                .excludePathPatterns("/patient/register")
                .excludePathPatterns("/patient/forget")
                .excludePathPatterns("/patient/login")
                .excludePathPatterns("/alipay/**");
    }
}
